package com.xindong.paking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindong.paking.dtos.ParkingLotDTO;
import com.xindong.paking.dtos.PageResult;
import com.xindong.paking.entity.Coupon;
import com.xindong.paking.entity.ParkingLot;
import com.xindong.paking.entity.ParkingLot;
import com.xindong.paking.mapper.ParkingLotMapper;
import com.xindong.paking.mapper.ParkingLotMapper;
import com.xindong.paking.service.ParkingLotService;
import com.xindong.paking.service.ParkingLotService;
import com.xindong.paking.utils.Result;
import com.xindong.paking.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.xindong.paking.entity.constant.Constants.*;

@Service
public class ParkingLotServiceImpl extends ServiceImpl<ParkingLotMapper, ParkingLot> implements ParkingLotService {

    @Autowired
    private ParkingLotMapper parkingLotMapper;
    @Override
    public Result queryByConForPage(ParkingLotDTO dto) {
        //获取LambdaQueryWrapper
        LambdaQueryWrapper<ParkingLot> queryWrapper = new LambdaQueryWrapper<>();

        //如果传入的数据都为空，则进行全查
        //若传入的名称不为空，则进行模糊查询
        queryWrapper.like(StringUtil.isNotEmpty(dto.getName()),ParkingLot::getName,dto.getName())
                .le(StringUtil.isNotEmpty(dto.getDes()),ParkingLot::getDes,dto.getDes())
                .le(StringUtil.isNotEmpty(dto.getUnitPrice()),ParkingLot::getUnitPrice,dto.getUnitPrice())
                .le(StringUtil.isNotEmpty(dto.getParkTotal()),ParkingLot::getParkTotal,dto.getParkTotal())
                .like(StringUtil.isNotEmpty(dto.getChargReQuatity()),ParkingLot::getChargReQuatity,dto.getChargReQuatity())
                .ge(StringUtil.isNotEmpty(dto.getChargTotal()),ParkingLot::getChargTotal,dto.getChargTotal())
                .ne(StringUtil.isNotEmpty(dto.getParkReQuatity()),ParkingLot::getParkReQuatity,dto.getParkReQuatity())
                .eq(StringUtil.isNotEmpty(dto.getStatus()),ParkingLot::getStatus,dto.getStatus())
                .eq(StringUtil.isNotEmpty(dto.getId()),ParkingLot::getId,dto.getId());


        //使用mybatis-plus的page进行分页查询
//        IPage<ParkingLot> ParkingLots = new Page<ParkingLot>(getCurrentPage(dto.getPageCount(), dto.getPageSize()), dto.getPageSize());
//        ParkingLots = this.page(ParkingLots, queryWrapper);
//        System.out.println("ParkingLots.getRecords() = " + ParkingLots.getRecords());
        IPage<ParkingLot> ParkingLots = new Page<>(dto.getPageCount(), dto.getPageSize());
        IPage<ParkingLot> parkingLotIPage = parkingLotMapper.selectPage(ParkingLots, null);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(parkingLotIPage.getRecords());
        pageResult.setTotal(pageResult.getTotal());
        pageResult.setPage(dto.getPageCount());
        pageResult.setPageSize(dto.getPageSize());
        System.out.println("parkingLotIPage.getRecords() = " + parkingLotIPage.getRecords());
        //封装结果
//        PageResult pageResult = new PageResult(ParkingLots.getRecords(),ParkingLots.getTotal(), (int) ParkingLots.getCurrent(), (int) ParkingLots.getSize());

        return Result.success(DEFAULT_QUERY_SUCCESS_MSG,pageResult);
    }

    @Override
    public Result deleteById(Integer ParkingLotId) {

        //如果传入的ParkingLotId为空时，返回报错
        if (StringUtil.isEmpty(ParkingLotId)){
            return Result.error("请输入需要删除的优惠券的id");
        }
        //根据传入id进行删除操作
        LambdaUpdateWrapper<ParkingLot> deleteWrapper = new LambdaUpdateWrapper<>();
        deleteWrapper.eq(ParkingLot::getId,ParkingLotId);
        boolean remove = remove(deleteWrapper);
        //如果成功，返回成功信息
        if (remove){
            return Result.success(DEFAULT_DELETE_SUCCESS_MSG,null);
        }
        //否则返回失败信息
        return Result.error(DEFAULT_DELETE_FAIL_MSG);
    }

    @Override
    public Result insertParkingLot(ParkingLot dto) {


        boolean save = save(dto);

        if (save){
            return Result.success(DEFAULT_INSERT_SUCCESS_MSG);
        }
        return Result.error(DEFAULT_INSERT_FAIL_MSG);
    }

    @Override
    public Result UpdateParkingLot(ParkingLot dto) {
        LambdaUpdateWrapper<ParkingLot> updatewrapper = new LambdaUpdateWrapper<>();
        updatewrapper
                .set(StringUtil.isNotEmpty(dto.getName()),ParkingLot::getName,dto.getName())
                //若传入的优惠券价值不为空，则进行<=查询
                .set(StringUtil.isNotEmpty(dto.getDes()),ParkingLot::getDes,dto.getDes())
                //若传入的优惠券类型不为空，则进行精确查询
                .set(StringUtil.isNotEmpty(dto.getUnitPrice()),ParkingLot::getUnitPrice,dto.getUnitPrice())
                //若传入的优惠券剩余数量不为空，则进行<=查询
                .set(StringUtil.isNotEmpty(dto.getParkTotal()),ParkingLot::getParkTotal,dto.getParkTotal())
                //若传入的优惠券描述不为空，则进行模糊查询
                .set(StringUtil.isNotEmpty(dto.getChargReQuatity()),ParkingLot::getChargReQuatity,dto.getChargReQuatity())
                //若传入的优惠券发放时间不为空，则进行>=查询
                .set(StringUtil.isNotEmpty(dto.getChargTotal()),ParkingLot::getChargTotal,dto.getChargTotal())
                //若传入的优惠券过期时间不为空，则进行<=查询
                .set(StringUtil.isNotEmpty(dto.getParkReQuatity()),ParkingLot::getParkReQuatity,dto.getParkReQuatity())
                //若传入的优惠券状态不为空，则进行精确查询
                .set(StringUtil.isNotEmpty(dto.getStatus()),ParkingLot::getStatus,dto.getStatus())
                .set(ParkingLot::getCreateTime,new Date())
                //若传入的优惠券id不为空，则进行精确查询
                .eq(StringUtil.isNotEmpty(dto.getId()),ParkingLot::getId,dto.getId());


        boolean update = update(updatewrapper);

        if (update){
            return Result.success(DEFAULT_INSERT_SUCCESS_MSG);
        }
        return Result.error(DEFAULT_INSERT_FAIL_MSG);
    }

    @Override
    public Result queryDetailById(Integer parkingLotId) {
        LambdaQueryWrapper<ParkingLot> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isEmpty(parkingLotId)){
            return Result.error("未输入id");
        }
        queryWrapper.eq(ParkingLot::getId,parkingLotId);
        List<ParkingLot> parkingLots = parkingLotMapper.selectList(queryWrapper);

        return Result.success(DEFAULT_QUERY_SUCCESS_MSG,parkingLots);
    }

    @Override
    public Result getList() {
        List<ParkingLot> parkingLots = parkingLotMapper.selectList(null);
        return Result.success("获取成功",parkingLots);
    }

    /**
     * 计算当前页是第几条数据
     * @param pageCount 当前页
     * @param pageSize 每页条数
     * @return
     */
    private Integer getCurrentPage(Integer pageCount, Integer pageSize) {
        return (pageCount - 1)*pageSize;
    }



}
